import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/views/Login"
import Home from "@/views/Home"
import { Toast } from 'vant'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "/login",
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      title: "登录"
    },
    component: Login
  },
  {
    path: "/register",
    name: "register",
    meta: {
      title: "注册"
    },
    component: () => import("@/views/Register.vue")
  },
  {
    path: "/home",
    name: "name",
    meta: {
      title: "首页",
      showFooter: true // 显示底部菜单栏
    },
    component: Home,
  }, {
    path: "/board",
    name: "board",
    meta: {
      title: "宠物寄养",
      showFooter: true // 显示底部菜单栏
    },
    component: () => import("@/views/Board.vue")
  },
  {
    path: "/order",
    name: "order",
    meta: {
      title: "订单",
      showFooter: true // 显示底部菜单栏
    },
    component: () => import("@/views/Order.vue")
  }, {
    path: "/mine",
    name: "mine",
    meta: {
      title: "我的",
      showFooter: true // 显示底部菜单栏
    },
    component: () => import("@/views/Mine.vue")
  }, {
    path: "/dailylist",
    name: "dailylist",
    meta: {
      title: "日志列表",
    },
    component: () => import("@/views/DailyList.vue")
  },
  {
    path: "/dailydetail",
    name: "dailydetail",
    meta: {
      title: "日志详情"
    },
    props: true,
    component: () => import("@/views/DailyDetial.vue")
  },
  {
    path: "/business/:b_id",
    name: "business",
    props: true,
    component: () => import("@/views/Business.vue")
  }, {
    path: "/petreservation/:b_id/:b_name/:b_price",
    // 开启路由接参
    props: true,
    name: "petreservation",
    meta: {
      title: "立即预定"
    },
    component: () => import("@/views/PetReservation.vue")
  }, {
    path: "/contactAdd",
    name: "contactAdd",
    meta: {
      title: "新增联系人"
    },
    component: () => import("@/views/ContactAdd.vue")
  }, {
    path: "/contactEdit/:id",
    name: "contactEdit",
    props: true,
    meta: {
      title: "编辑联系人"
    },
    component: () => import("@/views/ContactEdit.vue")
  }
  , {
    path: "/contactList",
    name: "contactList",
    meta: {
      title: "联系人列表"
    },
    component: () => import("@/views/ContactList.vue")
  },
  {
    path: "/pet",
    name: "pet",
    meta: {
      title: "宠物资料"
    },
    component: () => import("@/views/Pet.vue")
  }
]
// 用户希望当我查看玩详情页以后返回，返回列表页的位置是刚刚浏览的位置
const scrollBehavior = function scrollBehavior(to, from, savedPosition) {
  if (savedPosition) {

    return savedPosition;
  } else {
    return { x: 0, y: 0 }
  }
};
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  //scrollBehavior
})

router.beforeEach((to, from, next) => {
  if (to.path == "/login" || to.path == "/register") {
    next()
  } else {
    let token = sessionStorage.getItem("token")
    if (token) {
      next()
    } else {
      Toast.fail("请先登录！")
      next("/login")
    }
  }
})

router.afterEach((to, from) => {
  let bodySrcollTop = document.body.scrollTop
  if (bodySrcollTop !== 0) {
    document.body.scrollTop = 0
    return
  }
  let docSrcollTop = document.documentElement.scrollTop
  if (docSrcollTop !== 0) {
    document.documentElement.scrollTop = 0
  }
})

export default router
